package mj.scott.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mj.scott.common.Result;
import mj.scott.pojo.entity.Category;
import mj.scott.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 菜品分类
 * @author Scott
 * @since 2025-08-15
 */
@RestController
@RequestMapping("/category")
@Api(tags = "菜品分类")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 新增菜品分类
     * @param category 分类信息
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增菜品分类接口")
    public Result<Category> addCategory(@RequestBody Category category) {
        Category savedCategory = categoryService.saveCategory(category);
        return Result.success(savedCategory);
    }

    /**
     * 修改菜品分类
     * @param id 分类ID
     * @param category 新的分类信息
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @ApiOperation("修改菜品分类接口")
    public Result<Category> updateCategory(
            @PathVariable Long id,
            @RequestBody Category category) {
        // 确保ID一致性
        category.setId(id);
        Category updatedCategory = categoryService.updateCategory(category);
        return Result.success(updatedCategory);
    }

    /**
     * 删除菜品分类
     * @param id 分类ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除菜品分类接口")
    public Result<String> deleteCategory(@PathVariable Long id) {
        // 可以添加判断：如果分类下有菜品，不允许删除
        categoryService.deleteCategory(id);
        return Result.success("分类删除成功");
    }

    /**
     * 查询所有分类
     * @return 分类列表
     */
    @GetMapping
    @ApiOperation("查询所有菜品分类接口")
    public Result<List<Category>> getAllCategories() {
        List<Category> categories = categoryService.getAllCategories();
        return Result.success(categories);
    }

    /**
     * 用户端分类查询
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> getAllCategory() {
        return Result.success(categoryService.getAllCategory());
    }
}
